import time
import torch_geometric.utils
from torch_geometric.data import HeteroData
from torch_geometric.loader import LinkNeighborLoader
import torch
import networkx as nx
import matplotlib.pyplot as plt

import torch_geometric.transforms as T
from torch_geometric.nn import HGTConv, Linear
from origin_data import MyData

from load_data import alarms_df

data = MyData(alarms_df).data
# print(data)
data = T.ToUndirected()(data)


# print(data[("host", "belongsto", "bussiness_tree")])

def descrition_data(_data_dict, _data):
    _descrition_data = {}
    all_list = []
    vdata = _data.edge_label_index.t().tolist()
    # print(vdata)
    for i2j in vdata:
        all_list.append((i2j, _data_dict.all_dict[i2j[0]], _data_dict.all_dict[i2j[1]]))
    return all_list


transform = T.RandomLinkSplit(
    num_val=0.1,
    num_test=0.1,
    disjoint_train_ratio=0.0,
    neg_sampling_ratio=0.0,
    add_negative_train_samples=False,
    is_undirected=True,
    # split_labels=True,
    edge_types=[
        ("alarm", "on", "host"),
        ("alarm", "to", "bussiness_tree"),
        ("host", "belongsto", "bussiness_tree")
    ],
    # rev_edge_types=[
    #     ("host", "rev_on", "alarm"),
    #     ("bussiness_tree", "rev_to", "alarm"),
    #     ("bussiness_tree", "rev_belongsto", "host")
    # ]
)

# train_data, val_data, test_data = transform(data)
train_data, val_data, test_data = transform(data.to_homogeneous())
print("data........................................................................................................",
      data)
# print(
#     "train_data ........................................................................................................",
#     train_data)
# print(
#     "val_data ........................................................................................................",
#     val_data)
# print(
#     "test_data ........................................................................................................",
#     test_data)
desc_data = descrition_data(data, train_data)
# print(desc_data)
for i in desc_data:
    print(i)

print("train_data.edge_label_index.to(torch.long)",train_data.edge_label_index.t().tolist())
# # train_loader
train_loader = LinkNeighborLoader(
    data=train_data,
    num_neighbors=[2, 2],
    neg_sampling_ratio=0.0,
    edge_label_index=train_data.edge_label_index.to(torch.long),
    edge_label=train_data.edge_label.to(torch.long),
    batch_size=8,
    shuffle=True
)

#
for batch in train_loader:
    # print(batch)
    # print("batch.node_id...........",batch.node_id)
    # print("batch.node_type...........",batch.node_type)
    # print("batch.n_id...........", batch.n_id)
    # # print("batch.edge_type...........", batch.edge_type)
    # print("batch.edge_index...........", batch.edge_index)
    # print("batch.e_id...........", batch.edge_label_index)
    # print("batch.edge_label...........", batch.edge_label)
    for i in batch.edge_index.t().tolist():
        print("id1:{} id2:{}, {} #### {}".format(int(batch.n_id[i[0]]),int(batch.n_id[i[1]]),batch.all_dict[int(batch.n_id[i[0]])] ,batch.all_dict[int(batch.n_id[i[1]])]))

#     # print([j[0] for j in desc_data])
#     data = batch.edge_label_index.t().tolist()
#     for i in data:
#         print(i)
#         print(i in [j[0] for j in desc_data])
#     break


def draw(data):
    # 将 PyTorch Geometric 图转换为 NetworkX 图
    g = torch_geometric.utils.to_networkx(data, to_undirected=False)
    node_colors = ['red' for i in range(10)] + ['green' for i in range(10)] + ['blue' for i in range(10)]  # 对应每个节点的颜色
    # 绘制图形
    # nx.draw(g, pos=nx.spring_layout(g),with_labels=True,node_color=node_colors)
    # # 保存为 PNG 图像
    nx.draw_networkx_labels(g, nx.spring_layout(g), font_color="red")
    nx.draw_networkx_edge_labels(g, nx.spring_layout(g),
                                 font_color="blue")  # no edge_labels parameter, default is showing all attributes of edges
    # plt.show()
    plt.savefig("output.png")

"""
    0='10.10.112.44|http://10.10.112.44:9213/_cluster/health/?pretty 集群状态为yellow|北京亦庄电信通|LIVE_EPG_APP',
    1='10.29.130.21|Zabbix agent is unreachable for 20 minutes|陕西西安移动|LIVE_EPG_FCACHE',
    2='10.79.130.66|Zabbix agent is unreachable for 20 minutes|江西南昌移动|LIVE_EPG_DISPATCH,LIVE_FAVO_PROXY,LIVE_EPG_PROXY',
    3='10.79.130.36|Zabbix agent is unreachable for 20 minutes|江西南昌移动|VOD_EPG_PROXY',
    4='10.88.130.10|/data1 磁盘空间剩余量过小|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    5='10.10.112.60|系统交换分区 持续5分钟平均每秒换入100页|北京亦庄电信通|VOD_AUTH_APP,LIVE_AUTH_APP',
    6='10.24.130.14|Zabbix agent is unreachable for 20 minutes|辽宁沈阳移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    7='10.24.132.1|Zabbix agent is unreachable for 20 minutes|辽宁大连机房|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    8='10.24.132.2|Zabbix agent is unreachable for 20 minutes|辽宁大连机房|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    9='10.24.130.13|Zabbix agent is unreachable for 20 minutes|辽宁沈阳移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    10='10.31.130.21|/var/log/message里有 DOWN 异常|河北沧州移动|VOD_EPG_PROXY',
    11='10.31.130.20|/var/log/message里有 DOWN 异常|河北沧州移动|VOD_EPG_PROXY',
    12='10.31.130.21|eth2 出向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    13='10.31.130.21|eth3 出向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    14='10.31.130.20|eth2 入向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    15='111.62.41.77|111.62.41.77 is unavailable by ICMP|河北沧州移动|VOD_EPG_PROXY',
    16='111.62.41.79|111.62.41.79 is unavailable by ICMP|河北沧州移动|VOD_EPG_PROXY',
    17='111.62.41.78|111.62.41.78 is unavailable by ICMP|河北沧州移动|VOD_EPG_PROXY',
    18='111.62.41.80|111.62.41.80 is unavailable by ICMP|河北沧州移动|VOD_EPG_PROXY',
    19='10.31.130.20|eth3 入向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    20='10.31.130.20|eth2 出向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    21='10.31.130.20|eth3 出向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    22='10.31.130.21|eth2 入向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    23='10.31.130.21|eth3 入向流量平均5分钟大于90%|河北沧州移动|VOD_EPG_PROXY',
    24='10.31.130.20|keepalived VIP个数有变化，需确认|河北沧州移动|VOD_EPG_PROXY',
    25='10.31.130.21|keepalived VIP个数有变化，需确认|河北沧州移动|VOD_EPG_PROXY',
    26='10.29.130.20|/var/log/message里有 down 异常|陕西西安移动|LIVE_EPG_FCACHE',
    27='10.87.130.7|keepalived 进程数异常|云南呈贡移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    28='10.87.130.7|keepalived VIP个数有变化，需确认|云南呈贡移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    29='10.87.130.8|keepalived VIP个数有变化，需确认|云南呈贡移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    30='10.47.130.31|keepalived 进程数异常|内蒙古移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    31='10.47.130.27|keepalived VIP个数有变化，需确认|内蒙古移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    32='10.47.130.31|keepalived VIP个数有变化，需确认|内蒙古移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    33='10.88.130.10|nginx未shutting进程数持续5分钟大于0|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    34='10.87.130.7|nginx未shutting进程数持续5分钟大于0|云南呈贡移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    35='10.87.130.8|nginx未shutting进程数持续5分钟大于0|云南呈贡移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    36='10.29.130.24|nginx未shutting进程数持续5分钟大于0|陕西西安移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    37='10.10.112.44|/opt/soft/gad_elk_logstash/ 程序进程消失|北京亦庄电信通|LIVE_EPG_APP',
    38='10.29.130.23|nginx未shutting进程数持续5分钟大于0|陕西西安移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    39='10.10.112.60|系统交换分区 持续5分钟平均每秒换出100页|北京亦庄电信通|VOD_AUTH_APP,LIVE_AUTH_APP',
    40='10.88.130.10|10.88.130.10 is unavailable by ICMP|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    41='183.224.24.1|183.224.24.1 is unavailable by ICMP|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    42='10.88.130.10|Icmp Ping 丢包率连续5分钟超过20%|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    43='10.88.30.10|本机管理卡不通|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    44='10.88.130.10|Zabbix agent is unreachable for 20 minutes|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    45='10.88.130.10|rsyslogd 不可用|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    46='10.88.130.10|keepalived 进程数异常|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    47='10.88.130.11|keepalived VIP个数有变化，需确认|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    48='10.88.130.10|keepalived VIP个数有变化，需确认|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    49='10.88.130.10|monit 监控服务有异常，请检查/var/log/monit|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    50='10.88.30.10|iDRAC No SNMP data collection|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    51='10.88.130.10|/var/log/message里有 down 异常|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    52='10.88.130.11|10.88.130.11 is unavailable by ICMP|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    53='183.224.24.2|183.224.24.2 is unavailable by ICMP|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    54='10.88.130.11|bond0 速率降低|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    55='10.88.130.11|keepalived 进程数异常|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    56='10.88.130.11|Zabbix agent is unreachable for 20 minutes|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    57='10.88.30.11|本机管理卡不通|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    58='10.88.30.11|iDRAC No SNMP data collection|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    59='10.88.130.89|monit 监控服务有异常，请检查/var/log/monit|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    60='10.88.130.10|服务器已经被重启|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    61='10.88.130.11|服务器已经被重启|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    62='10.88.130.89|服务器已经被重启|云南昆明移动|LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY',
    63='10.47.130.19|端口:37018 mongodb-rec-内蒙 Creshed  没有采集到数据！|内蒙古移动|LIVE_EPG_SCACHE',
    64='10.47.130.19|端口:47017 lanmu-mongodb-history-内蒙-02 Creshed  没有采集到数据！|内蒙古移动|LIVE_EPG_SCACHE',
    65='10.47.130.19|10.47.130.19 is unavailable by ICMP|内蒙古移动|LIVE_EPG_SCACHE',
    66='10.47.130.19|http://10.47.130.19:9200/_cluster/health/?pretty 集群状态为yellow|内蒙古移动|LIVE_EPG_SCACHE',
    67='10.47.130.19|10.47.130.19:1984/cp/galaxy/tv/filter?mac=8c%3Aba%3A25%3Af4%3A90%3A64&partner_code=HBYD A000000  应用访问返回异常|内蒙古移动|LIVE_EPG_SCACHE',
    68='10.47.130.19|Zabbix agent is unreachable for 20 minutes|内蒙古移动|LIVE_EPG_SCACHE',
    69='10.47.130.19|eth3 网卡down|内蒙古移动|LIVE_EPG_SCACHE',
    70='10.47.130.19|eth2 网卡down|内蒙古移动|LIVE_EPG_SCACHE',
    71='10.47.130.18|http://10.47.130.18:9200/_cluster/health/?pretty 集群状态为yellow|内蒙古移动|LIVE_EPG_SCACHE',
    72='10.47.130.19|/var/log/message里有 down 异常|内蒙古移动|LIVE_EPG_SCACHE',
    73='10.47.130.19|服务器已经被重启|内蒙古移动|LIVE_EPG_SCACHE',
    74='10.87.130.7|nginx未shutting进程数持续5分钟大于0|云南呈贡移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    75='10.87.130.8|nginx未shutting进程数持续5分钟大于0|云南呈贡移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    76='10.47.130.31|nginx未shutting进程数持续5分钟大于0|内蒙古移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    77='10.10.112.60|系统交换分区 持续5分钟平均每秒换入100页|北京亦庄电信通|LIVE_AUTH_APP,VOD_AUTH_APP',
    78='10.24.130.14|eth4 网卡down|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    79='10.24.130.14|bond0 速率降低|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    80='10.24.130.13|eth5 网卡down|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    81='10.24.130.13|bond0 速率降低|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    82='10.24.130.14|/var/log/message里有 down 异常|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    83='10.24.130.13|/var/log/message里有 down 异常|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    84='10.24.130.14|bond0 入向流量平均5分钟大于90%|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    85='10.24.130.14|bond0 出向流量平均5分钟大于90%|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    86='221.180.243.9|221.180.243.9 is unavailable by ICMP|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    87='10.24.130.13|keepalived VIP个数有变化，需确认|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    88='10.24.130.14|keepalived VIP个数有变化，需确认|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    89='10.24.130.14|/var/log/message里有 DOWN 异常|辽宁沈阳移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    90='10.29.130.23|/var/log/message里有 DOWN 异常|陕西西安移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    91='10.10.112.60|/data 磁盘空间剩余量过小|北京亦庄电信通|LIVE_AUTH_APP,VOD_AUTH_APP',
    92='10.25.132.103|/data1 磁盘空间剩余量过小|江苏扬州移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    93='10.29.130.19|出现 CRITICAL DISK|陕西西安移动|LIVE_EPG_FCACHE',
    94='10.29.30.19|iDRAC Physical Disk 0:1:5: Physical disk is in warning state|陕西西安移动|LIVE_EPG_FCACHE',
    95='10.29.130.19|出现 DEGR  DISK|陕西西安移动|LIVE_EPG_FCACHE',
    96='10.29.130.24|/var/log/message里有 DOWN 异常|陕西西安移动|LIVE_UPGRADE_PROXY,LIVE_PIC_PROXY',
    97='10.29.130.19|端口:47017 mongodb-3.2-lanmu-陕西主-单点 Creshed  没有采集到数据！|陕西西安移动|LIVE_EPG_FCACHE',
    98='10.29.130.21|端口:47017 mongodb-3.2-lanmu-陕西主-单点 Creshed  没有采集到数据！|陕西西安移动|LIVE_EPG_FCACHE',
    99='10.29.130.20|端口:37018 mongodb-rec-陕西主 Creshed  没有采集到数据！|陕西西安移动|LIVE_EPG_FCACHE',
    100='10.87.130.8',
    101='10.29.30.19',
    102='10.47.130.18',
    103='10.88.30.10',
    104='10.88.130.89',
    105='10.10.112.60',
    106='10.31.130.21',
    107='111.62.41.78',
    108='10.87.130.7',
    109='10.88.130.10',
    110='10.31.130.20',
    111='10.24.132.2',
    112='10.47.130.19',
    113='10.88.130.11',
    114='111.62.41.79',
    115='10.29.130.24',
    116='10.24.132.1',
    117='10.47.130.27',
    118='10.24.130.13',
    119='10.29.130.23',
    120='10.25.132.103',
    121='10.24.130.14',
    122='10.88.30.11',
    123='221.180.243.9',
    124='10.29.130.19',
    125='183.224.24.2',
    126='10.29.130.21',
    127='10.10.112.44',
    128='10.79.130.66',
    129='10.79.130.36',
    130='111.62.41.80',
    131='183.224.24.1',
    132='10.47.130.31',
    133='111.62.41.77',
    134='10.29.130.20',
    135='LIVE_EPG_SCACHE',
    136='LIVE_EPG_APP',
    137='VOD_AUTH_APP',
    138='LIVE_FAVO_PROXY',
    139='LIVE_AUTH_APP',
    140='VOD_EPG_PROXY',
    141='LIVE_UPGRADE_PROXY',
    142='LIVE_EPG_FCACHE',
    143='LIVE_EPG_PROXY',
    144='LIVE_EPG_DISPATCH',
    145='LIVE_PIC_PROXY',

135='LIVE_EPG_SCACHE'
73='10.47.130.19|服务器已经被重启|内蒙古移动|LIVE_EPG_SCACHE',


112='10.47.130.19'
73='10.47.130.19|服务器已经被重启|内蒙古移动|LIVE_EPG_SCACHE',

72='10.47.130.19|/var/log/message里有 down 异常|内蒙古移动|LIVE_EPG_SCACHE',
135='LIVE_EPG_SCACHE'

67='10.47.130.19|10.47.130.19:1984/cp/galaxy/tv/filter?mac=8c%3Aba%3A25%3Af4%3A90%3A64&partner_code=HBYD A000000  应用访问返回异常|内蒙古移动|LIVE_EPG_SCACHE'
135='LIVE_EPG_SCACHE'

64='10.47.130.19|端口:47017 lanmu-mongodb-history-内蒙-02 Creshed  没有采集到数据！|内蒙古移动|LIVE_EPG_SCACHE',
112='10.47.130.19'

67='10.47.130.19|10.47.130.19:1984/cp/galaxy/tv/filter?mac=8c%3Aba%3A25%3Af4%3A90%3A64&partner_code=HBYD A000000  应用访问返回异常|内蒙古移动|LIVE_EPG_SCACHE'
112='10.47.130.19'

135='LIVE_EPG_SCACHE'
72='10.47.130.19|/var/log/message里有 down 异常|内蒙古移动|LIVE_EPG_SCACHE',

135='LIVE_EPG_SCACHE'
112='10.47.130.19'

112='10.47.130.19'
72='10.47.130.19|/var/log/message里有 down 异常|内蒙古移动|LIVE_EPG_SCACHE',
"""